using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Threading.Tasks;
using Abp.Application.Services;
using Abp.Authorization.Users;
using Abp.Domain.Entities.Auditing;
using Abp.IdentityFramework;
using Abp.Runtime.Session;
using Abp.Threading;
using Microsoft.AspNetCore.Identity;
using Yozeev.Authorization.Roles;
using Yozeev.BusinessLogic.DomainService;
using Yozeev.MultiTenancy;
using Yozeev.SystemConfig;
using Yozeev.SystemConfig.BasicConfig;
using Yozeev.SystemConfig.BasicConfig.DomainService;
using Yozeev.SystemConfig.DomainService;
using Yozeev.UserManagerment.Users;

namespace Yozeev
{
    /// <summary>
    ///    项目应用程序服务的基类。
    /// </summary>
    public abstract class YozeevAppServiceBase : ApplicationService
    {
        protected YozeevAppServiceBase()
        {
            LocalizationSourceName = AppConsts.LocalizationSourceName;
        }

        public TenantManager TenantManager { get; set; }

        public UserManager UserManager { get; set; }

        public BranchUserManager BranchUserManager { get; set; }

        public PowerRoleManager PowerRoleManager { get; set; }

        public TicketUserEnableManager TicketUserEnableManager { get; set; }

        public TicketRoleManager TicketRoleManager { get; set; }

        public RoleManager RoleManager { get; set; }

        public BranchManager BranchManager { get; set; }

        /// <summary>
        ///     返回当前用户信息
        /// </summary>
        /// <returns></returns>
        protected virtual async Task<User> GetCurrentUserAsync()
        {
            var user = await UserManager.FindByIdAsync(AbpSession.GetUserId().ToString());
            if (user == null) throw new Exception("当前用户不存在!");

            return user;
        }
        /// <summary>
        /// 返回当前用户信息--非异步
        /// </summary>
        /// <returns></returns>
        protected virtual User GetCurrentUser()
        {
            return AsyncHelper.RunSync(GetCurrentUserAsync);
        }


        /// <summary>
        ///     返回当前租户信息
        /// </summary>
        /// <returns></returns>
        protected virtual async Task<Tenant> GetCurrentTenantAsync()
        {
            return await TenantManager.GetByIdAsync(AbpSession.GetTenantId());
        }

        protected virtual void CheckErrors(IdentityResult identityResult)
        {
            identityResult.CheckErrors(LocalizationManager);
        }
        /// <summary>
        /// 获取当前用户的机构用户
        /// </summary>
        /// <returns></returns>
        protected virtual async Task<BranchUser> GetBranchUser()
        {
            return await BranchUserManager.GetBranchUser(GetCurrentUser());
        }
        /// <summary>
        /// 查询当前用户的数据查看类型
        /// </summary>
        /// <typeparam name="T">菜单名称类</typeparam>
        /// <returns></returns>
        protected virtual ViewTypeEnum GetViewTypeEnum<T>()
        {
            return PowerRoleManager.GetViewType<T>(AbpSession.GetUserId());
        }
        /// <summary>
        /// 根据当前用户查询可售票价Id列表
        /// </summary>
        /// <returns></returns>
        protected virtual List<Guid> GetTicketPriceIdList()
        {
            return TicketRoleManager.GetTicketPriceIdList(AbpSession.GetUserId());
        }
        /// <summary>
        /// 设置机构和用户Id
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="entity"></param>
        /// <returns></returns>
        protected async Task SetBranchUser<T>(T entity) where T: ByBranchEntity<Guid>
        {
            var branchUser = await GetBranchUser();
            entity.CreatorUserId = AbpSession.GetUserId();
            if (branchUser == null) return;
            entity.BranchId = branchUser.BranchId;
        }
        /// <summary>
        /// 根据用户权限生成菜单查询条件
        /// </summary>
        /// <returns></returns>
        protected Expression<Func<Menu, bool>> GetMenuExpressionByUser()
        {
            if (AbpUserBase.AdminUserName == GetCurrentUser().UserName)
                return entity => 1 == 1;
            return PowerRoleManager.GetMenuExpressionByUser(AbpSession.GetUserId());
        }

        protected List<string> GetPowerCodeByUser(Guid menuId)
        {
            return PowerRoleManager.GetPowerCodeByUser(AbpSession.GetUserId(),menuId);
        }
        /// <summary>
        /// 获取默认机构
        /// </summary>
        /// <returns></returns>
        protected virtual async Task<Guid> GetBranchId()
        {

            var branchUser = await GetBranchUser();
            Guid branchId;
            if (branchUser == null)
                branchId = BranchManager.GetBranchFirst().Id;
            else
                branchId = branchUser.BranchId;
            return branchId;
        }
    }
}